<template>
  <div class="login-container">
    <video id="bg-video" src="../../assets/video.mp4" autoplay muted loop />
    <el-form ref="form" :model="loginForm" :rules="rules">
      <div class="img">
        <img src="@/assets/logo1.png" alt="">
      </div>
      <el-form-item prop="account">
        <div class="box">
          <el-input
            v-model="loginForm.account"
            class="account"
            placeholder="请输入账号"
          />
        </div>
      </el-form-item>
      <el-form-item prop="password">
        <div class="box">
          <el-input
            v-model="loginForm.password"
            class="psd"
            placeholder="请输入密码"
            show-password
          />
        </div>
      </el-form-item>
      <el-form-item prop="code">
        <div class="box,Box">
          <el-input
            v-model="loginForm.code"
            placeholder="验证码"
            class="code"
          />
        </div>
        <div class="imgCode" @click="getImgCode1">
          <img :src="imgUrl">
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleClick">登录</el-button>
      </el-form-item>
      <div class="content">
        仅用于IT培训教学使用，为保障您的个人信息安全，请勿向平台录入任何个人敏感信息（如手机号、身份证号等）!
      </div>
    </el-form>
    <div class="version">
      <span>江苏传智播客教育科技股份有限公司</span>
      <span
        style="margin-left: 20px"
      >版权所有Copyright 2006-2022 All Rights Reserved</span>
      <span style="margin-left: 20px,height:50px">苏ICP备16007882号-11</span>
      <!-- <el-button @click="getImgCode2">点我</el-button> -->
    </div>

  </div>
</template>

<script>
import { imgCode } from '@/api/user'
import debounce from '@/utils/debounce'
export default {
  name: 'Login',
  data() {
    return {
      imgUrl: '',
      loginForm: {
        account: process.env.NODE_ENV === 'development' ? 'demo' : '',
        password: process.env.NODE_ENV === 'development' ? 'hm#qd@23!' : '',
        code: '',
        key: Math.floor(Math.random() * 10)
      },
      rules: {
        account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '请输入6-16位密码', trigger: 'blur' }
        ],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getImgCode()
  },
  methods: {
    handleClick() {
      this.$refs.form.validate(async(isOk) => {
        if (isOk) {
          const res = await this.$store.dispatch('user/login', this.loginForm)
          // console.log(res)
          if (res.code !== 200) {
            this.getImgCode()
            this.$message({
              type: 'error',
              message: res.msg,
              duration: 1000
            })
          } else {
            this.$message({
              type: 'success',
              message: '登录成功',
              duration: 1000
            })
            this.$router.push('/')
          }
        }
      })
    },
    getImgCode1: debounce(function() {
      this.getImgCode()
      // this.handleClick()
    }, 1000),
    async getImgCode() {
      // console.log(this.imgUrl)
      const data = await imgCode(this.loginForm)
      // console.log(data)
      //    blob转base64
      const reader = new FileReader()
      reader.readAsDataURL(data.data)
      reader.onload = (e) => {
        this.imgUrl = e.target.result
        // console.log(e.target.result)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100vh;
  height: 100%;
  background: url("../../assets/video.mp4");
  opacity: 0.9;
  #bg-video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  .el-form {
    position: absolute;
    top: 45%;
    left: 45%;
    background-color: #fff;
    margin: -180px 0 0 -160px;
    width: 450px;
    height: 524px;
    padding: 36px 40px;
    border-radius: 10px;
    .img {
      text-align: center;
      margin-bottom: 30px;
      img {
        width: 180px;
        height: 54px;
      }
    }
    ::v-deep .el-input__inner {
      height: 50px;
      padding-left: 48px;
      border-radius: 5px;
    }
    .im {
      width: 80px;
      height: 30px;
    }

    .imgCode {
      position: absolute;
      right: 4px;
      top: 4px;
    }

    .el-button {
      width: 100%;
      height: 50px;
      margin-top: 20px;
    }
    .content {
      font-size: 14px;
      color: #ff0000;
      line-height: 40px;
    }
    .box {
      position: relative;
      .account::before {
        content: "";
        position: absolute;
        top: 15px;
        left: 15px;
        width: 21px;
        height: 21px;
        background: url("../../assets/icon-account.png") no-repeat center / 21px
          21px;
      }
      .psd::before {
        content: "";
        position: absolute;
        top: 15px;
        left: 15px;
        width: 21px;
        height: 21px;
        background: url("../../assets/icon-password.png") no-repeat center /
          21px 21px;
      }
    }
    .code::before {
      content: "";
      position: absolute;
      top: 15px;
      left: 15px;
      width: 21px;
      height: 21px;
      background: url("../../assets/icon-code.png") no-repeat center / 21px 21px;
    }
  }
  .version {
    font-size: 14px;
    color: white;
    text-align: center;
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    opacity: 0.9;
  }
}
</style>
